package com.googlecode.classgrep.visitor.invoker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.objectweb.asm.Label;
import org.objectweb.asm.commons.EmptyVisitor;

import com.googlecode.classgrep.entity.invoker.LocalVariableSignature;

/**
 * 
 * fetch info(such as : start line number, start line number, desc, name and so on) 
 * for method local variable
 *
 */
public class LocalVariableInfoFetchVisitor extends EmptyVisitor {
	
	/**
	 * current label line number
	 */
	protected int lineNumber;

	/**
	 * line number for each label
	 */
	protected Map<Label, Integer> labelLineMap = new HashMap<Label, Integer>();
	
	
	/**
	 * 
	 */
	List<LocalVariableSignature> varSigns;
	

	/**
	 * 
	 * @param varSigns
	 */
	public LocalVariableInfoFetchVisitor(List<LocalVariableSignature> varSigns) {
		this.varSigns = varSigns;
	}
	
	

	@Override
	public void visitLabel(Label label) {
		labelLineMap.put(label, lineNumber++);
	}
	
	@Override
	public void visitLocalVariable(String name, String desc,
			String signature, Label start, Label end, int index) {
		LocalVariableSignature sign = new LocalVariableSignature();
		sign.setName(name);
		sign.setDesc(desc);
		sign.setSignature(signature);
		sign.setStartLine(labelLineMap.get(start));
		sign.setEndLine(labelLineMap.get(end));
		sign.setIndex(index);
		varSigns.add(sign);
	}
	
}